<template>
  <div class="fullscreen-container">
    <div class="content-wrapper">
      <el-card class="login-card">
      <template #header>
        <div class="login-title">用户注册</div>
      </template>
      <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="registerForm.username" placeholder="请输入用户名" prefix-icon="User" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="registerForm.password" type="password" placeholder="请输入密码" prefix-icon="Lock" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input v-model="registerForm.confirmPassword" type="password" placeholder="请再次输入密码" prefix-icon="Lock" />
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select v-model="registerForm.roleId" placeholder="请选择角色">
            <el-option label="教师" value="2" />
            <el-option label="考生" value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="registerForm.realName" placeholder="请输入真实姓名" prefix-icon="Document" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="registerForm.gender">
            <el-radio value="男">男</el-radio>
            <el-radio value="女">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="registerForm.phone" placeholder="请输入手机号" prefix-icon="Phone" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="registerForm.email" placeholder="请输入邮箱" prefix-icon="Message" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleRegister" style="width: 100%">注册</el-button>
        </el-form-item>
        <el-form-item>
          <div style="text-align: center; margin-top: 10px;">
            已有账号？<el-link type="primary" @click="goToLogin">立即登录</el-link>
          </div>
        </el-form-item>
      </el-form>
      </el-card>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { postJson } from '../utils/api'

const router = useRouter()
const registerFormRef = ref()

const registerForm = reactive({
  username: '',
  password: '',
  confirmPassword: '',
  roleId: '',
  realName: '',
  gender: '',
  phone: '',
  email: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 50, message: '用户名长度在 3 到 50 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule: any, value: string, callback: any) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ],
  roleId: [
    { required: true, message: '请选择角色', trigger: 'change' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ],
  phone: [
    {
      pattern: /^1[3-9]\d{9}$/,
      message: '请输入正确的手机号',
      trigger: 'blur'
    }
  ],
  email: [
    {
      type: 'email',
      message: '请输入正确的邮箱地址',
      trigger: 'blur'
    }
  ]
}

const handleRegister = async () => {
  try {
    // 表单验证
    await registerFormRef.value.validate()
    
    // 准备请求数据
    const userData = {
      username: registerForm.username,
      password: registerForm.password, // 实际项目中可能需要前端加密
      roleId: parseInt(registerForm.roleId),
      realName: registerForm.realName,
      gender: registerForm.gender || '',
      phone: registerForm.phone || '',
      email: registerForm.email || ''
    }
    
    // 使用API工具类发送注册请求
    const result = await postJson('/user/register', userData)
    
    if (result.code === 200) {
      ElMessage.success('注册成功，请登录')
      // 注册成功后跳转到登录页
      router.push('/login')
    } else {
      ElMessage.error(result.msg || '注册失败，请重试')
    }
  } catch (error) {
    console.error('注册请求失败:', error)
    ElMessage.error('网络错误，请检查后端服务是否正常运行')
  }
}

const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.fullscreen-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background-color: #f0f2f5;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  overflow: auto;
}

.content-wrapper {
  width: 100%;
  max-width: 1200px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.login-card {
  background: white;
  width: 600px;
  max-width: 100%;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .fullscreen-container {
    padding: 10px;
  }
  
  .login-card {
    width: 100%;
  }
}

.login-title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  padding: 30px 0;
  margin: 0;
  color: #303133;
}

/* 表单样式 */
.el-form {
  padding: 0 40px 30px;
}

.el-form-item {
  margin-bottom: 24px;
}

.el-form-item__label {
  width: 80px;
  font-size: 14px;
}

.el-form-item__content {
  margin-left: 80px !important;
}

.el-input__wrapper,
.el-select__wrapper {
  border-radius: 4px;
  font-size: 14px;
}

.el-input__inner,
.el-select__input {
  height: 36px;
  font-size: 14px;
}

.el-button--primary {
  width: 100%;
  height: 36px;
  font-size: 14px;
  margin-top: 10px;
}

/* 单选按钮样式 */
.el-radio-group {
  display: flex;
  gap: 20px;
}

/* 链接样式 */
.el-link {
  cursor: pointer;
}
</style>